package com.study.d012Components;

import com.sun.javafx.scene.web.skin.HTMLEditorSkin;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.HTMLEditor;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**WebView 浏览器
*@description
*@author xtayhame
*date 2020年3月18日
*version
*since v1.0
*/
public class WebViewMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane= new AnchorPane();
		HBox hBox = new HBox(30);
		Button forwardButton = new Button("前进");
		Button backButton = new Button("后退");
		Button infoButton = new Button("信息");
		hBox.getChildren().addAll(forwardButton,backButton,infoButton);

		WebView web = new WebView();
		WebEngine engine= web.getEngine();
		engine.load("http://www.baidu.com");

		//历史记录
		WebHistory history= engine.getHistory();

		//设置缩放
		//web.setZoom(0.5);
		web.setPrefSize(540, 300);
		//HTMLEditorSkin skin = new HTMLEditorSkin(editor);

		anchorPane.getChildren().addAll(hBox,web);

		AnchorPane.setTopAnchor(hBox, 10.0);
		AnchorPane.setLeftAnchor(hBox, 10.0);
		AnchorPane.setTopAnchor(web, 30.0);
		AnchorPane.setLeftAnchor(web, 10.0);

		stage.setScene(new Scene(anchorPane, 600, 400));
		stage.show();

		//宽和高绑定
		web.prefHeightProperty().bind(stage.heightProperty());
		web.prefWidthProperty().bind(stage.widthProperty().subtract(10).subtract(forwardButton.getHeight()));


		infoButton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				System.out.println("获取当前索引："+history.getCurrentIndex());
				System.out.println("获取标题："+engine.getTitle());
				System.out.println("获取代理："+engine.getUserAgent());
				System.out.println("获取样式："+engine.getUserStyleSheetLocation());
			}
		});

		forwardButton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				try {
					history.go(1);
				} catch (Exception e) {

				}
			}
		});
		backButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				try {
					history.go(-1);
				} catch (Exception e) {

				}
			}
		});
	}
}
